package com.adapter;

import java.util.List;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.dstats.R;
import com.entities.Hero;
import com.fetch.ImageLoader;

public class HeroAdapter extends BaseAdapter {

	 //Lista de heroes!
		private List<Hero> heroes;
		private LayoutInflater mInflater;
		private Context mContext;
		private static ImageLoader imageLoader;
	//	private ViewHolder holder;
	 
	 
		static class ViewHolder{
			private ImageView img;
			private TextView heroName;			
		}
	 
	 
		public HeroAdapter(Context context, List<Hero> heroes) {
			mInflater = LayoutInflater.from(context);
			this.heroes = heroes;
			mContext = context;
			if(imageLoader==null)
				imageLoader =  new ImageLoader(mContext);
		}
	 
		@Override
		public int getCount() {
			return heroes.size();
		}
	 
		@Override
		public Hero getItem(int index) {
			return heroes.get(index);
		}
	 
		@Override
		public long getItemId(int index) {
			
			return heroes.get(index).getId();
		}
	 
		@Override
		public View getView(int posicao, View convertView, ViewGroup arg2) {
	
			ViewHolder holder;
			if (convertView == null) {
				convertView = mInflater.inflate(R.layout.hero_view_row, null);
				holder = new ViewHolder();  
				holder.heroName = (TextView) convertView.findViewById(R.id.textViewHeroName);
				holder.img = (ImageView) convertView.findViewById(R.id.imageViewHeroImg);
	            convertView.setTag(holder);
			} else {
				holder = (ViewHolder) convertView.getTag();
			}
	 		Hero h = heroes.get(posicao);
	 
			holder.heroName.setText(h.getLocalized_name());
			holder.heroName.setGravity(Gravity.CENTER);
			

			int rescale_h = 82;
			int rescale_w = 154;
			int imgIdentifier = mContext.getResources().getIdentifier(h.getImgResString(), "drawable", mContext.getPackageName());
			if(imgIdentifier!=0){
			Bitmap bm = BitmapFactory.decodeResource(mContext.getResources(), imgIdentifier);
			holder.img.setImageBitmap(Bitmap.createScaledBitmap(bm, rescale_w, rescale_h, false));
			} else {
			imageLoader.DisplayImage(h.getImgLgUrl(), holder.img, rescale_h, rescale_w);
			}
	 
			return convertView;
		}
	 

}